package chenjiuxu.seniorui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import java.util.Timer;
import java.util.TimerTask;
/**
 * Created by 15705 on 2018/1/19.
 * 线形计时器
 */
public class LineTimerView extends View {


    private final Paint paint;
    private int maxTime = 10000;//表示时间 毫秒
    private int smallTime = 5000;//最小时间
    private int thisTime = 0;//当前时间刻度


    private int bgColor = Color.RED;//背景线颜色
    private int okColor = Color.BLACK;//计时线颜色
    private int dotColor = Color.BLUE;//点颜色

    private int padding = 20;
    private int width = -1;//宽
    private int height = -1;//高
    private float thisSize = 0;//当前
    private float smallPx;
    private Timer timer;//计时器
    private TimerTask timerTask;//计时任务
    private OnTimerListener listener;

    public LineTimerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);

        paint.setStrokeCap(Paint.Cap.ROUND);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (width == -1) {
            width = getWidth();
            height = getHeight();
            smallPx = time2px(smallTime);
        }
        paint.setStrokeWidth(20);
        paint.setColor(bgColor);

        int h = height / 2;

        canvas.drawLine(thisSize + padding, h, width - padding, h, paint);//背景
        paint.setColor(okColor);
        canvas.drawLine(0 + padding, h, thisSize + padding, h, paint);//已用进度

        paint.setStrokeWidth(10);
        paint.setColor(dotColor);
        canvas.drawLine(padding + smallPx, h - 10, padding + smallPx, h + 10, paint);

    }

    /**
     * 时间换px
     */
    private float time2px(int time) {
        return (width - 2 * padding) * ((float) time / maxTime);
    }


    /**
     * 开始计时
     */
    public void startTime() {
        thisTime = 0;
        thisSize = 0;
        if (timer == null) timer = new Timer();//计时器

        timerTask = new TimerTask() {
            @Override
            public void run() {
                thisTime += 25;
                if (thisTime >= maxTime) {
                    cancel();//取消任务
                    timer.purge();//清空计时器
                    listener.stopListener(true);
                }
                thisSize = time2px(thisTime);
                invalidate();
            }
        };

        timer.schedule(timerTask, 0, 25);
    }

    /**
     * 停止计时
     */
    public void stopTime() {
        timer.purge();
        if(thisTime>=smallTime){
            listener.stopListener(true);
        }else {
            listener.stopListener(false);
        }
        if (timerTask != null) timerTask.cancel();
        timerTask = null;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        timer.cancel();
    }

    /**
     * 设置时间
     */
    public void setMaxTime(int maxTime) {
        this.maxTime = maxTime;
    }

    /**
     * 最小时间
     */
    public void setSmallTime(int smallTime) {
        this.smallTime = smallTime;
        invalidate();
    }

    /**
     * 监听
     */
    public interface OnTimerListener {

        void stopListener(boolean issmallTime);//计时结束监听
    }

    public void setOnTimerListener(OnTimerListener listener) {

        this.listener = listener;
    }

}
